﻿using System;
using System.ComponentModel;
using System.Xml;
using Aeon.Configuration;
using Aeon.Emulator.Dos.VirtualFileSystem;

namespace Aeon.DiskImages
{
    /// <summary>
    /// CD-ROM drive configuration.
    /// </summary>
    [DriveConfiguration("Aeon.HostCDDrive")]
    public sealed class HostCDDriveConfiguration : AeonConfiguration, IDriveConfiguration
    {
        private string volume;

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the HostCDDriveConfiguration class.
        /// </summary>
        public HostCDDriveConfiguration()
        {
        }
        #endregion

        #region Public Static Properties
        /// <summary>
        /// Gets the name of the configuration type.
        /// </summary>
        public static string Name
        {
            get { return "Host CD-ROM drive"; }
        }
        #endregion

        #region Public Properties
        /// <summary>
        /// Gets a value indicating whether the drive is read-only.
        /// </summary>
        public bool IsReadOnly
        {
            get { return true; }
        }
        /// <summary>
        /// Gets or sets the host volume to use.
        /// </summary>
        public string Volume
        {
            get { return this.volume; }
            set
            {
                if(value != this.volume)
                {
                    this.volume = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("Volume"));
                }
            }
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Returns a new instance of the emulated drive.
        /// </summary>
        /// <returns>New instance of the emulated drive.</returns>
        public IMappedDrive Create()
        {
            return new HostCDDrive(this.Volume);
        }
        /// <summary>
        /// Returns a deep copy of the drive configuration.
        /// </summary>
        /// <returns>Deep copy of the drive configuration.</returns>
        public IDriveConfiguration Clone()
        {
            return new HostCDDriveConfiguration()
            {
                volume = this.volume
            };
        }
        #endregion

        #region Protected Methods
        /// <summary>
        /// Serializes the configuration to an XML element.
        /// </summary>
        /// <param name="xmlWriter">XmlWriter used to serialize the object.</param>
        protected override void Serialize(XmlWriter xmlWriter)
        {
            if(xmlWriter == null)
                throw new ArgumentNullException("xmlWriter");

            xmlWriter.WriteAttributeString("Volume", this.Volume);
        }
        /// <summary>
        /// Deserializes the configuration from an XML element.
        /// </summary>
        /// <param name="xmlReader">XmlReader used to deserialize the object.</param>
        protected override void Deserialize(XmlReader xmlReader)
        {
            if(xmlReader == null)
                throw new ArgumentNullException("xmlReader");

            this.Volume = xmlReader.GetAttribute("Volume");
        }
        #endregion
    }
}
